package com.wxm.utils;

import com.wxm.exception.TlPayException;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Set;



/**
 * <p>
 *      hibernate-validator校验工具类
 * </p>
 *
 * @author: wxm
 * @Date: 2023/4/14
 */
public class ValidatorUtils {
    private static final Validator validator;

    static {
        validator = Validation.buildDefaultValidatorFactory().getValidator();
    }

    /**
     * 校验对象
     * @param object 待校验对象
     * @param groups 待校验的组
     */
    public static void validateEntity(Object object, Class<?>... groups)  {
        Set<ConstraintViolation<Object>> constraintViolations = validator.validate(object, groups);
        if (!constraintViolations.isEmpty()) {
        	ConstraintViolation<Object> constraint = (ConstraintViolation<Object>)constraintViolations.iterator().next();
            R.exception("参数校验失败：" + constraint.getMessage());
        }
    }
}
